function render() {
	$('.options-div input').iCheck({
		checkboxClass : 'icheckbox-blue',
		radioClass : 'iradio-blue',
		increaseArea : '20%'
	});
}
$(function(){
	var scale_id=localStorage.getItem("scale_id");
	var sel_type=localStorage.getItem("sel_type");
	if(sel_type!="论述题自测"){
		$('[value="论述题"]').hide();
	}else{
		$('[value="单选题"]').hide();
		$('[value="多选题"]').hide();
	}
	var url="../EditScaleServlet";
	$.post(url,"scale_id="+scale_id,function(result){
		var questions=JSON.parse(result);
		for(var i in questions){
			sessionStorage.setItem('q'+(Number(i)+1),JSON.stringify(questions[i]));
			add_left_btn();
		}
		left_btn_init();
		$('.menu_scale').first().trigger('click');
	});
})
function appendText(index) {
	var txt1 = "<div class='options-div'><div class='radio-box'><input type='radio' id='radio-"
			+ index
			+ "' name='radio1' value='"
			+ String.fromCharCode(64 + index)
			+ "'><label for='radio-"
			+ index
			+ "'>"
			+ String.fromCharCode(64 + index)
			+ "</label></div><div class='check-box hidden'><input type='checkbox' id='checkbox-"
			+ index
			+ "' name='checkbox1' value='"
			+ String.fromCharCode(64 + index)
			+ "'><label for='checkbox-"
			+ index
			+ "'>"
			+ String.fromCharCode(64 + index)
			+ "</label></div><textarea class='textarea radius textarea-options' rows='' cols=''></textarea></div>"; // 以
	// HTML
	// 创建新元素
	$(".options-box").append(txt1); // 追加新元素
	render();
	render1();
}
$(function() {

	render();
	$('.select-box')[0].onchange = function() {
		render1();
	}

	$('.add-op-btn').on('click', function() {
		addOption()
	});
	$('.delete-op-btn').on('click', function() {
		deleteOption()
	});
})
function addOption() {
	var index = $('.options-div').length + 1;
	if (index < 20) {
		appendText(index);
	} else
		alert("选项添加已达上限，如需添加，请联系管理员！");

}
function deleteOption() {
	if ($('.options-div').length > 2) {
		$(".options-div:last").remove();
	} else {
		alert("已经是最少选项了");
	}
}
function render1() {

	if ($('.select')[0].value == "单选题") {
		$('.check-box').each(function() {
			this.classList.add('hidden');
		})
		$('.radio-box').each(function() {
			this.classList.remove('hidden');
		});
		$('.op-answer-box').each(function() {
			this.classList.remove('hidden');
		})
		$('.discuss-answer-box').each(function() {
			this.classList.add('hidden');
		})
	}
	;
	if ($('.select')[0].value == "多选题") {
		$('.radio-box').each(function() {
			this.classList.add('hidden');
		})
		$(".check-box").each(function() {
			this.classList.remove('hidden');
		})
		$('.op-answer-box').each(function() {
			this.classList.remove('hidden');
		})
		$('.discuss-answer-box').each(function() {
			this.classList.add('hidden');
		})
	}
	;
	if ($('.select')[0].value == "论述题") {
		$('.op-answer-box').each(function() {
			this.classList.add('hidden');
		})
		$('.discuss-answer-box').each(function() {
			this.classList.remove('hidden');
		})
	}
	;
}
function left_btn_init() {
	$('.menu_scale').on(
			"click",
			function() {
				var o_question = sessionStorage.getItem($(this).attr('name'));
				if (o_question) {
					var json_q = JSON.parse(o_question);
					showContent(json_q.question_type, json_q.question, json_q.question_answer,
							json_q.options, json_q.reference_score, json_q.difficulty_rank);
					$('.add-btn').addClass('hidden');
					$('.upload-btn').addClass('hidden');
					$('.modify-btn').removeClass('hidden');
					$('.delete-btn').removeClass('hidden');
					$('.newbtn').removeClass('hidden');
					$('form').find('h3')
							.html("第" + ($(this).index() - 1) + "题");
				}
			});
}

function showContent(type, question, answer, options, score, difficulty) {
	$("[name='question']")[0].value = question;
	$('.select')[0].value = type;
	render1();
	if (type == "单选题") {
		$('.iradio-blue').find('input').each(function() {
			if ($(this).val() == answer[0]) {
				$(this).next().trigger("click")
				$("#" + $(this).attr("id")).parent().addClass('checked');
			}
		})
		render();
		showAnswer(options, score, difficulty);
	}
	if (type == "多选题") {
		for ( var a in answer) {
			$('.icheckbox-blue').find('input').each(function() {
				if ($(this).val() == answer[a])
					$(this).attr('checked', '');
			})
		}
		render();
		showAnswer(options, score, difficulty);
	}
	if (type == "论述题") {
		$("[name='discuss_answer']")[0].value = answer[0];
	}

}
function showAnswer(options, score, difficulty) {
	var answer_op_num = options.length;
	var page_op_num = $('.options-div').length;
	while (page_op_num > answer_op_num) {
		deleteOption();
		page_op_num = $('.options-div').length;
	}
	while (page_op_num < answer_op_num) {
		addOption();
		page_op_num = $('.options-div').length;
	}
	if (page_op_num == answer_op_num) {
		for ( var o in options) {
			var op;
			for ( var i in options[o]) {
				op = i;
			}
			$('.iradio-blue')
					.find('input')
					.each(
							function() {
								if ($(this).val() == op) {
									$(this).parent().parent().parent().find(
											'textarea')[0].value = options[o][op];
								}
							})
			// $(options[o].op).parent().parent().parent().find('textarea')[0].value=options[o].content;
		}
	}
	$("[name='score']")[0].value = score;
	$("[name='difficulty']")[0].value = difficulty;
}
$(function() {
	//增加题目按钮
	$('.add-btn').on(
			"click",
			function() {
				add_left_btn();
			});
	//修改题目按钮
	$('.modify-btn').on("click", function() {
		var scale_num = $('form').find('h3').html()[1];
		var question = $("[name='question']")[0].value;
		setSessionStorage(scale_num, question);
		$('.menu_scale p')[scale_num - 1].innerHTML = question;
	})
	
	// 批量导入按钮
	$('.upload-btn').on("click", function() {
		$('#bcg_editor').show();
	})
})
function add_left_btn(){
	var scale_num = $('.menu_scale').length + 1;
	var question = JSON.parse(sessionStorage.getItem("q" + scale_num))["question"];
	/*setSessionStorage(scale_num, question);*/
	var left_btn = "<dl class='menu_scale' name='q" + scale_num
			+ "'><li><a><span>第" + scale_num + "题</span><p>"
			+ question.substr(0,18) + "</p></a></li></dl>"
	$(".menu_dropdown").append(left_btn);
	$('form').find('h3').html("第" + (scale_num + 1) + "题");
	/*resetPage();*/
	
}
function resetPage() {
	left_btn_init();
	$(':reset').trigger('click');
	$('.options-box').find('.checked').each(function() {
		$(this).removeClass('checked');
	});
	render1();
}
/*function setSessionStorage(scale_num, question) {
	var buff = {};
	buff["scale_num"] = scale_num;
	buff["question"] = question;
	var type = $("[name='op_select']")[0].value;
	buff["type"] = type;
	var score = $("[name='score']")[0].value;
	buff["score"] = score;
	var difficulty = $("[name='difficulty']")[0].value;
	buff["difficulty"] = difficulty;
	var t_options = $('.textarea-options');
	var options = [];
	var obj = {};
	for (var i = 0; i < t_options.length; i = i + 1) {
		obj[String.fromCharCode(65 + i)] = t_options[i].value;
		options.push(obj);
		obj = {};
	}
	buff["options"] = options;
	var answer = [];
	if (type == "单选题") {
		$('.iradio-blue.checked').find('input').each(function() {
			answer.push($(this).val());
		});
	}
	if (type == "多选题") {
		$('.icheckbox-blue.checked').find('input').each(function() {
			answer.push($(this).val());
		});
	}
	if (type == "论述题") {
		answer.push($("[name='discuss_answer']").val());
	}
	buff["answer"] = answer;
	sessionStorage.setItem("q" + scale_num, JSON.stringify(buff));
}
$('.newbtn').on('click', function() {
	var scale_num = $('.menu_scale').length + 1;
	$('form').find('h3').html("第" + (scale_num) + "题");
	$(this).addClass('hidden');
	$('.modify-btn').addClass('hidden');
	$('.delete-btn').addClass('hidden');
	$('.add-btn').removeClass('hidden');
	$('.upload-btn').removeClass('hidden');
	resetPage();

});
$('.delete-btn').on('click', function() {
	var dst_num = $('form').find('h3').html()[1] - 1;
	$('.menu_scale li').eq(dst_num).remove()
	$('.menu_scale li').each(function(index) {
		$(this).find('a').find('span').html("第" + (index + 1) + "题");
	})
	var scale_num = $('.menu_scale li').length + 1;
	$('form').find('h3').html("第" + (scale_num) + "题");
	$(this).addClass('hidden');
	$('.modify-btn').addClass('hidden');
	$('.delete-btn').addClass('hidden');
	$('.add-btn').removeClass('hidden');
	$('.upload-btn').removeClass('hidden');
	resetPage();
})*/

// 编辑窗口取消按钮
$(function() {
	$('#btn_exit').on('click', function() {
		$("#i_path").val("");
		$("#bcg_editor").hide();
	});

});
/*将题目列表中的题目插入题库*/
$('.import_btn').on('click',function(){
	var request_q={};
	$('.menu_scale').each(function(index){
		name=$(this).attr('name');
		var question_info=sessionStorage.getItem(name)
		if(question_info){
			request_q[name]=question_info;
		}else alert('操作超时，请重新插入');
	})
	console.log(request_q);
	/*目标servlet*/
	var url="../ImportQuestionsServlet";
	$.post(url,"questions="+JSON.stringify(request_q),function(result){
		/*var res = JSON.parse(result);
		if (res.status == 'error') {
			if(!confirm('试题导入失败！是否重新导入？')){
				location.href = res.value;
			}
		}
		if (res.status == 'success') {
			alert("试题导入成功！");
			location.href = res.value;
		}*/
	})
})
function check() {
	var path = $("#i_path").val();
	var suffix = path.substr(path.indexOf(".") + 1);
	if (suffix != "xls" && suffix != "xlsx") {
		alert("导入文件格式非法");
		return false;
	}
	return true;
}
/*category_id
function onsubmit(){
	var t_options = $('.textarea-options');
	var obj = {};
	var type=$('form')[0].question_type.value;
	for (var i = 0; i < t_options.length; i = i + 1) {
		obj[String.fromCharCode(65 + i)] = t_options[i].value;
	}
	var answer = [];
	if (type == "单选题") {
		$('.iradio-blue.checked').find('input').each(function() {
			answer.push($(this).val());
		});
	}
	if (type == "多选题") {
		$('.icheckbox-blue.checked').find('input').each(function() {
			answer.push($(this).val());
		});
	}
	if (type == "论述题") {
		answer.push($("[name='discuss_answer']").val());
	}
	$('form')[0].options=JSON.stringify(obj);
	var url="../ImportQuestionsServlet";
	$.post(url,
			"question="+$('form')[0].question.value+
			"&options="+JSON.stringify(obj)+
			"&reference_score="+$('form')[0].reference_score.value+
			"&question_answer="+answer+
			"&difficulity_rank="+$('form')[0].difficulity_rank.value+
			"&question_type="+type
	)
}*/